﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Api.Common
{
    public class CommonUrlProvide : BaseProvider
    {
        public override RouteData GetMyRouteData(HttpContextBase httpContext)
        {
            //~/{controller}/{action}/{channelId}? 这个通用格式匹配
            if (Th.IsCusDomain)
            {
                var regex = new Regex(@"~/([a-zA-Z]*)/([a-zA-Z]*)/*(\d*)\?*");
                var match = regex.Match(VirtualPath);
                if (match.Success)
                {
                    var data = new RouteData(this, new MvcRouteHandler());
                    data.Values.Add("controller", match.Groups[1].Value);
                    data.Values.Add("action", match.Groups[2].Value);
                    data.Values.Add("siteid", SiteId);
                    data.Values.Add("channelid", match.Groups[3].Value);
                    return data;
                }
            }
            return null;
        }

        public override VirtualPathData GetMyVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            //返回{controller}/{action}/{channelId}?这个格式，不显示siteId
            if (Th.IsCusDomain)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}/{1}", VirtualControllerName, VirtualActionName);
                if (!string.IsNullOrEmpty(VirtualChannelId))
                {
                    sb.AppendFormat("/{0}", VirtualChannelId);
                }

                sb.AppendFormat(VirtualOther);
                string url = sb.ToString();
                return new VirtualPathData(this, url);
            }

            return null;
        }

    }
}